﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace PayrollExpress.Models
{
    public static class ReportHelper
    {
        
        #region Query Models



        #endregion

        #region Excel

        public static ExcelWorksheet SetWorksheet(ExcelPackage package, string workBookName, string firstHeaderCenteredText, 
            int defaultColumnWidth = 20, eOrientation orientation = eOrientation.Landscape, bool showGridLines = false,
            ePaperSize paperSize = ePaperSize.Legal, bool verticalCentered = false, bool horizontalCentered = true,
            bool pageLayoutView = true, int zoomScale = 90)
        {

            var ws = package.Workbook.Worksheets.Add(workBookName);

            ws.DefaultColWidth = defaultColumnWidth;
            ws.PrinterSettings.Orientation = orientation;
            ws.PrinterSettings.ShowGridLines = showGridLines;
            ws.PrinterSettings.PaperSize = paperSize;
            ws.PrinterSettings.VerticalCentered = verticalCentered;
            ws.PrinterSettings.HorizontalCentered = horizontalCentered;
            ws.View.PageLayoutView = pageLayoutView;
            ws.View.ZoomScale = 90;
            ws.HeaderFooter.FirstHeader.CenteredText = firstHeaderCenteredText;

            return ws;
        }

        public static ExcelWorksheet SetWorksheetHeader(ExcelWorksheet ws, List<string> headerNames,
            char columnStart = 'A', int rowStart = 1)
        {
            var column = columnStart;

            for (var columnValue = 0; columnValue < headerNames.Count(); columnValue++)
            {
                ws.Cells[column + (rowStart).ToString()].Value = headerNames[columnValue];

                ws.Cells[column + (rowStart).ToString()].Style.Fill.PatternType = ExcelFillStyle.Solid;
                ws.Cells[column + (rowStart).ToString()].Style.Fill.BackgroundColor.SetColor(Color.LightGray);
                ws.Cells[column + (rowStart).ToString()].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                ws.Cells[column + (rowStart).ToString()].AutoFitColumns();

                ++column;
            }

            return ws;
        }

        public static ExcelWorksheet SetWorksheetRows(ExcelWorksheet ws, List<string> headerValues, char nextColumn, int nextRow)
        {
            
            for (var value = 0; value < headerValues.Count(); value++)
            {
                ws.Cells[nextColumn + nextRow.ToString()].Value = headerValues[value];
                ++nextColumn;
            }

            ws.Cells.AutoFitColumns();

            return ws;
        }

        private static bool IsFileLocked(FileInfo file)
        {
            FileStream stream = null;

            try
            {
                stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                stream.Close();
            }
            catch (IOException)
            {
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            return false;
        }

        #endregion

        #region PDF

        public static void ExportToPDF(string html, string path, iTextSharp.text.Rectangle pageSize, float marginLeft = 80f, float marginRight = 80f, float marginTop = 40f, float marginBottom = 80f)
        {
            Byte[] bytes;

            using (var ms = new MemoryStream())
            {
                using (var doc = new Document())
                {
                    using (var writer = PdfWriter.GetInstance(doc, ms))
                    {
                        doc.Open();
                        doc.SetPageSize(pageSize);
                        doc.SetMargins(marginLeft, marginRight, marginTop, marginBottom);
                        doc.NewPage();

                        using (var srHtml = new StringReader(html))
                        {
                            iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                        }

                        doc.Close();
                    }
                }

                bytes = ms.ToArray();
            }

            var newFileInfo = new FileInfo(path);
            if (!newFileInfo.Directory.Exists) Directory.CreateDirectory(newFileInfo.Directory.FullName);
            System.IO.File.WriteAllBytes(path, bytes);
            System.Diagnostics.Process.Start(path);
        }

        public static string GetHtmlString(HtmlTable table)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter tw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(tw);

            table.RenderControl(hw);

            return sb.ToString();
        }

        public static HtmlTable GetTable(bool setBorder = true)
        {
            var table = new HtmlTable();

            table.CellPadding = 0;
            table.CellSpacing = 0;
            table.Style.Add("display", "table");
            if (setBorder)
                table.Style.Add("border", "1px solid #EBEBEB");
            table.Style.Add("width", "100%");
            table.Style.Add("border-collapse", "collapse");
            table.Style.Add("font-family", "Helvetica, Arial, sans-serif");
            table.Style.Add("font-size", "11px");

            return table;
        }

        public static HtmlTable SetHeaderValues(HtmlTable table, HtmlTableRow header, List<string> headerValues, int columnSpan = 1, int rowSpan = 1, string textAlign = "left")
        {
            for (var i = 0; i < headerValues.Count(); i++)
            {
                var headerCell = new HtmlTableCell();

                headerCell.InnerText = headerValues[i];

                headerCell.Style.Add("background-color", "#F5F5F6");
                headerCell.Style.Add("border", "1px solid #e7e7e7");
                headerCell.Style.Add("padding", "8px");
                headerCell.Style.Add("text-align", textAlign);
                headerCell.Style.Add("font-weight", "bold");
                headerCell.Style.Add("height", "20px");

                headerCell.ColSpan = columnSpan;
                headerCell.RowSpan = rowSpan;

                header.Cells.Add(headerCell);
            }

            table.Rows.Add(header);

            return table;
        }

        public static HtmlTable SetRowValues(HtmlTable table, HtmlTableRow row, List<string> rowValues,
            int columnSpan = 1, int rowSpan = 1, bool setBorder = true, string textAlign = "left", 
            int paddingLeft = 0, int paddingRight = 0, int fontSize = 12)
        {
            for (var i = 0; i < rowValues.Count(); i++)
            {
                var rowCell = new HtmlTableCell();

                rowCell.InnerText = rowValues[i];

                rowCell.Style.Add("height", "20px");
                rowCell.Style.Add("text-align", textAlign);
                rowCell.Style.Add("background-color", "#ffffff");
                rowCell.Style.Add("font-size", fontSize.ToString() + "px");

                if (paddingLeft > 0)
                    rowCell.Style.Add("padding-left", paddingLeft.ToString() + "px");

                if (paddingRight > 0)
                    rowCell.Style.Add("padding-right", paddingRight.ToString() + "px");

                if (setBorder)
                    rowCell.Style.Add("border", "1px solid #e7e7e7");

                

                rowCell.ColSpan = columnSpan;
                rowCell.RowSpan = rowSpan;

                row.Cells.Add(rowCell);

            }

            table.Rows.Add(row);

            return table;
        }

        #endregion

    }
}